package com.zecan.blog.service.impl;

import com.zecan.blog.common.status.ClassifyStatus;
import com.zecan.blog.controller.exception.ClassifyException;
import com.zecan.blog.dao.ClassifyMapper;
import com.zecan.blog.entity.Article;
import com.zecan.blog.entity.Classify;
import com.zecan.blog.service.ClassifyService;
import com.zecan.blog.vo.article.ArticleMsgVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ClassifyServiceImpl
 * @Description
 * @date 2022/11/3 17:57
 * @Version 1.0
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Resource
    private ClassifyMapper classifyMapper;


    @Override
    public List<Classify> selectClassifyAll() {
        return  classifyMapper.findClassifyAll();
    }


    @Override
    public  List<ArticleMsgVo> selectArticleListByClassifyId(Integer classifyId,String classifyName,Integer offset, Integer limit) {

        Integer classifyIsExits = classifyMapper.findClassifyIsExits(classifyId);
        if(classifyIsExits.equals(0)) {
            throw  new ClassifyException(ClassifyStatus.CLASSIFY_IS_NOT_FOUND);
        }

        List<Article> list = classifyMapper.findArticleListByClassifyName(classifyName,offset,limit);
        if (list.size() == 0) {
            throw  new ClassifyException(ClassifyStatus.CLASSIFY_IS_NOT_COUNT);
        }

        List<ArticleMsgVo> articleMsgVoList = new ArrayList<>();

        // 数据封装
        for (Article article : list) {
            articleMsgVoList.add(ArticleMsgVo.buildArticleMsgVo(article,false,false,false));
        }
        return articleMsgVoList;
    }

    @Override
    public Integer getClassifyListCount() {
        return classifyMapper.selectClassifyCount();
    }

    @Override
    public void insertClassify(String newClassifyName) {
        classifyMapper.insertClassify(newClassifyName);
    }

    @Override
    public void updateClassifyName(Integer classifyId, String newClassifyName) {
        classifyMapper.updateClassifyNameById(classifyId, newClassifyName);
    }

    @Override
    public void deleteClassify(Integer classifyId) {
        classifyMapper.deleteClassifyById(classifyId);
    }
}
